<?php
namespace app\index\controller;
use think\Controller;

class Pdfsoap extends Controller
{
    public function GetPdf($cmlabsBarcode='61E015009867'){
        $key = $this->GetAuthorCode();
        try {
            $client = new \SoapClient("http://api.whcmlabs.com/CMLABSReportWebService.asmx?WSDL",
                array(
                    "stream_context" => stream_context_create(
                        array(
                            'ssl' => array(
                                'verify_peer' => false,
                                'verify_peer_name' => false,
                            )
                        )
                    )
                )
            );
            $parm = array('cmlabsBarcode' => $cmlabsBarcode, 'key' => $key);
            $result = $client->GetSearchByteSample($parm);

            $datas = (object_array($result))['GetSearchByteSampleResult']['Data']['enc_value']['ReportData']['Data'];
            $img = base64_decode($datas);
            $a = file_put_contents('test.pdf', $img);//返回的是字节数  //放置路径的文件名后缀可跟需求改变而改变
            print_r($a);
            exit();
        } catch (SOAPFault $e) {
            print $e;
        }
    }
    public function GetAuthorCode($userName='lis', $pwd='.....')
    {
        header("content-type:text/html;charset=utf-8");
        try {
            $client = new \SoapClient("http://api.whcmlabs.com/CMLABSReportWebService.asmx?WSDL",
                array(
                    "stream_context" => stream_context_create(
                        array(
                            'ssl' => array(
                                'verify_peer' => false,
                                'verify_peer_name' => false,
                            )
                        )
                    )
                )
            );
            $parm = array('userName' => $userName, 'pwd' => $pwd);
            $result = $client->GetAuthorCode($parm);
            //数据转换  object -> array
            return $data = (object_array($result))['GetAuthorCodeResult']['Data'];

        } catch (SOAPFault $e) {
            print $e;
        }

    }

    /*
     * @param $urls根据地址返回对应的  $client
     */
    public function ReturnClientByUrl($url)
    {
        libxml_disable_entity_loader(false);
        $opts = array(
            'http' => array(
                'user_agent' => 'PHPSoapClient'
            ),
            'ssl' => array(
                'verify_peer' => false
            ),
        );
        $context = stream_context_create($opts);

        $requestUrl = $url;
        //捕获下异常
        try {
            $client = new \SoapClient($requestUrl, array("trace" => 1, "cache_wsdl" => WSDL_CACHE_NONE, "exception" => 1, 'stream_context' => $context));
        } catch (Exception $e) {
            die($e->getMessage());
        }

        return $client;
    }

}